package snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel_2p extends JPanel implements KeyListener , ActionListener {
    //定义🐍蛇的数据结构
    int length;
    int[] snakeX = new int[600];
    int[] snakeY = new int[500];
    String fx;
    //定义第二只🐍
    int lengthR;
    int[] snakeRX = new int[600];
    int[] snakeRY = new int[500];
    String fxR;

    boolean isStart = false;
    boolean P_1Win = false;
    boolean P_2Win = false;

    //食物的坐标
    int foodx;
    int foody;
    Random random =new Random();

    //得分
    int score;
    //定时器 1000ms执行了10次
    Timer timer = new Timer(100,this);  //已毫秒为单位

    //构造器
    public GamePanel_2p() {
        init();
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start(); //游戏一开始定时器启动
    }

    //初始化方法
    public void init(){
        length = 3;
        fx = "R";
        snakeX[0] = 100;snakeY[0] = 100;    //脑袋
        snakeX[1] = 75;snakeY[1] = 100;    //第一节身体
        snakeX[2] = 50;snakeY[2] = 100;    //第二节身体

        //第二只蛇
        lengthR = 3;
        fxR = "R";
        snakeRX[0] = 100;snakeRY[0] = 600;    //脑袋
        snakeRX[1] = 75;snakeRY[1] = 600;    //第一节身体
        snakeRX[2] = 50;snakeRY[2] = 600;    //第二节身体

        //把食物随机分配到界面上
        foodx = 25 + 25*random.nextInt(34);
        foody = 75 + 25*random.nextInt(24);

        score = 0;

    }

    //绘制面板,我们游戏中的所有东西都用这只画笔
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.white);
        Data.header.paintIcon(this,g,25,11);        //头部广告栏
        g.fillRect(25,75,850,600);      //游戏界面

        //把小蛇画上去
        if(fx == "R")
            Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        else if(fx == "L")
            Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        else if(fx == "U")
            Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
        else if(fx == "D")
            Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);

        for (int i = 1; i < length; i++) {
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }
        //把第二只小蛇画上去
        if(fxR == "R")
            Data.right.paintIcon(this,g,snakeRX[0],snakeRY[0]);
        else if(fxR == "L")
            Data.left.paintIcon(this,g,snakeRX[0],snakeRY[0]);
        else if(fxR == "U")
            Data.up.paintIcon(this,g,snakeRX[0],snakeRY[0]);
        else if(fxR == "D")
            Data.down.paintIcon(this,g,snakeRX[0],snakeRY[0]);

        for (int i = 1; i < lengthR; i++) {
            Data.bodyRed.paintIcon(this,g,snakeRX[i],snakeRY[i]);
        }

        if(!isStart){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按 下 空 格 开 始 游 戏",250,300);
            g.drawString("让你的对手撞到你或自己则获胜",220,375);
            g.drawString("或者吃到很多食物...",275,450);
            g.drawString("按下 J 或者 回车键 使用技能",185,500);
            g.drawString("向前冲三格但会丢失一节身体",183,550);
        }
        if(P_2Win||P_1Win){
            g.setColor(Color.RED);
            g.setFont(new Font("微软雅黑",Font.BOLD,30));
            g.drawString("游 戏 结 束!",290,150);
            if(P_2Win){
                g.drawString("2P 赢得了游戏!",260,250);
            }else{
                g.drawString("1P 赢得了游戏!",260,250);
            }
            g.drawString("1P 长 度: "+length,295,350);
            g.drawString("2P 长 度: "+lengthR,295,400);
            g.drawString("按 下 空 格 重 新 游 戏",250,500);
        }
        //画积分
        g.setColor(Color.WHITE);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("1 P 长度: "+length,750,35);
        g.drawString("2 P 长度: "+lengthR,750,55);

        Data.food.paintIcon(this,g,foodx,foody);

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_SPACE) {
            if(P_2Win||P_1Win){
                //重新开始
                P_2Win = false;
                P_1Win = false;
                init();
            }else{
                isStart = !isStart;
            }
            repaint();
        }
        if(keyCode == KeyEvent.VK_UP && fx != "D") {
            fx = "U";
        }
        if(keyCode == KeyEvent.VK_DOWN&& fx != "U") {
            fx = "D";
        }
        if(keyCode == KeyEvent.VK_LEFT&& fx != "R") {
            fx = "L";
        }
        if(keyCode == KeyEvent.VK_RIGHT&& fx != "L") {
            fx = "R";
        }

        if(keyCode == KeyEvent.VK_W && fxR != "D") {
            fxR = "U";
        }
        if(keyCode == KeyEvent.VK_S&& fxR != "U") {
            fxR = "D";
        }
        if(keyCode == KeyEvent.VK_A&& fxR != "R") {
            fxR = "L";
        }
        if(keyCode == KeyEvent.VK_D&& fxR != "L") {
            fxR = "R";
        }
        if(keyCode == KeyEvent.VK_J&&lengthR>=3) {
            lengthR--;
            if(fxR.equals("R")) {
                snakeRX[0] += 75;
            }
            else if(fxR.equals("L")) {
                snakeRX[0] -= 75;
            }
            else if(fxR.equals("U")) {
                snakeRY[0] -= 75;
            }
            else if(fxR.equals("D")) {
                snakeRY[0] += 75;
            }
        }
        if(keyCode == KeyEvent.VK_ENTER && length>=3) {
            length--;
            if(fx.equals("R")) {
                snakeX[0] += 75;
            }
            else if(fx.equals("L")) {
                snakeX[0] -= 75;
            }
            else if(fx.equals("U")) {
                snakeY[0] -= 75;
            }
            else if(fx.equals("D")) {
                snakeY[0] += 75;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(isStart && !P_2Win && !P_1Win){        //如果游戏是开始状态 小蛇就动
            if(snakeX[0] == foodx && snakeY[0] == foody){
                length++;
                score+=10;
                foodx = 25 + 25*random.nextInt(34);
                foody = 75 + 25*random.nextInt(24);
                if(length >= 50){
                    P_1Win = true;
                }
            }
            if(snakeRX[0] == foodx && snakeRY[0] == foody){
                lengthR++;
                score+=10;
                foodx = 25 + 25*random.nextInt(34);
                foody = 75 + 25*random.nextInt(24);
                if(lengthR >= 50){
                    P_2Win = true;
                }
            }
            for (int i = length - 1; i > 0; i--) {
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];            //后一节移动到前一节
            }
            for (int i = lengthR - 1; i > 0; i--) {
                snakeRX[i] = snakeRX[i - 1];
                snakeRY[i] = snakeRY[i - 1];            //后一节移动到前一节
            }
            if(fx.equals("R")) {
                snakeX[0] += 25;
            }
            else if(fx.equals("L")) {
                snakeX[0] -= 25;
            }
            else if(fx.equals("U")) {
                snakeY[0] -= 25;
            }
            else if(fx.equals("D")) {
                snakeY[0] += 25;
            }

            if(fxR.equals("R")) {
                snakeRX[0] += 25;
            }
            else if(fxR.equals("L")) {
                snakeRX[0] -= 25;
            }
            else if(fxR.equals("U")) {
                snakeRY[0] -= 25;
            }
            else if(fxR.equals("D")) {
                snakeRY[0] += 25;
            }

            if(snakeX[0]> 850){
                snakeX[0] = 25;
            }
            else if(snakeX[0] < 25){
                snakeX[0] = 850;
            }
            if(snakeY[0] > 650){
                snakeY[0] = 75;
            }
            else if(snakeY[0] < 75){
                snakeY[0] = 650;
            }

            if(snakeRX[0]> 850){
                snakeRX[0] = 25;
            }
            else if(snakeRX[0] < 25){
                snakeRX[0] = 850;
            }
            if(snakeRY[0] > 650){
                snakeRY[0] = 75;
            }
            else if(snakeRY[0] < 75){
                snakeRY[0] = 650;
            }
            for (int i = 1; i < length; i++) {
                if(snakeY[0] == snakeY[i]&&snakeX[0] == snakeX[i]){
                    P_2Win = true;
                }
                if(snakeRY[0] == snakeY[i]&&snakeRX[0] == snakeX[i]){
                    P_1Win = true;
                }
            }

            for (int i = 1; i < lengthR; i++) {
                if(snakeRY[0] == snakeRY[i]&&snakeRX[0] == snakeRX[i]){
                    P_1Win = true;
                }
                if(snakeY[0] == snakeRY[i]&&snakeX[0] == snakeRX[i]){
                    P_2Win = true;
                }
            }
            repaint();
        }
        timer.start();
    }
}
